package com.bootdo.shop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.shop.domain.CategoryDO;
import com.bootdo.shop.service.CategoryService;
import com.bootdo.system.domain.DeptDO;
import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 产品分类表
 * 
 * @author yhg
 * @date 2017-12-14 14:42:35
 */
 
@Controller
@RequestMapping("/shop/category")
public class CategoryController  extends BaseController {
	
	private String prefix = "shop/category";
	@Autowired
	private CategoryService categoryService;
	
	@GetMapping()
	@RequiresPermissions("shop:category:category")
	String Category(){
	    return "shop/category/category";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:category:category")
	public List<CategoryDO> list(){
	/*	//查询列表数据
        Query query = new Query(params);
		List<CategoryDO> categoryList = categoryService.list(query);
		int total = categoryService.count(query);
		PageUtils pageUtils = new PageUtils(categoryList, total);
		return pageUtils;*/
		Map<String, Object> query = new HashMap<>(16);
		List<CategoryDO> categoryList = categoryService.list(query);
		return categoryList;
	}
	
	@GetMapping("/add/{pId}")
	@RequiresPermissions("shop:category:add")
	String add(@PathVariable("pId") Long pId, Model model){
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "顶级分类");
		} else {
			model.addAttribute("pName", categoryService.get(pId).getName());
		}
	    return "shop/category/add";
	}

	@GetMapping("/edit/{categoryId}")
	@RequiresPermissions("shop:category:edit")
	String edit(@PathVariable("categoryId") Long categoryId,Model model){
		CategoryDO category = categoryService.get(categoryId);
		model.addAttribute("category", category);
		if(Constant.DEPT_ROOT_ID.equals(category.getParentId())) {
			model.addAttribute("parentCategoryName", "无");
		}else {
			CategoryDO parDept = categoryService.get(category.getParentId());
			model.addAttribute("parentCategoryName", parDept.getName());
		}
	    return "shop/category/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:category:add")
	public R save( CategoryDO category){
		if(categoryService.save(category)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:category:edit")
	public R update( CategoryDO category){
		categoryService.update(category);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:category:remove")
	public R remove( Long categoryId){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", categoryId);
		if(categoryService.count(map)>0) {
			return R.error(1, "包含下级分类,不允许修改");
		}
		//删除商品类型跟其他的关系表操作，待完善
		if(categoryService.remove(categoryId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:category:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] categoryIds){
		categoryService.batchRemove(categoryIds);
		return R.ok();
	}
	@GetMapping("/tree")
	@ResponseBody
	public Tree<CategoryDO> tree() {
		Tree<CategoryDO> tree = new Tree<CategoryDO>();
		tree = categoryService.getTree();
		return tree;
	}

	@GetMapping("/treeView")
	String treeView() {
		return  prefix + "/categoryTree";
	}
}
